package com.cqc.lite.web;

import com.cqc.lite.domain.Permission;
import com.cqc.lite.domain.Role;
import com.cqc.lite.domain.UserInfo;
import com.cqc.lite.service.PermissionService;
import com.cqc.lite.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role/")
public class RoleController {

    private final RoleService roleService;
    private final PermissionService permissionService;
    @Autowired
    public RoleController(RoleService roleService,PermissionService permissionService){
        this.roleService=roleService;
        this.permissionService=permissionService;
    }

    @GetMapping("findAll.do")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("role-list");
        List<Role> roleList = roleService.findAll();
        mv.addObject("roleList",roleList);
        return mv;
    }


    @PostMapping("save.do")
    public String save(Role role){
        int a=roleService.save(role);
        return "redirect:/role/findAll.do";
    }

    @GetMapping("findById.do")
    public ModelAndView findById(String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("role-show");
        Role role = roleService.findById(id);
        mv.addObject("role",role);
        return mv;
    }


    @GetMapping("findRoleByIdAndAllPermission.do")
    public ModelAndView findRoleByIdAndAllPermission(String id){
        //1查找user
        Role role = roleService.findById(id);
        //获取当前角色的权限
        List<Permission> currPermList = role.getPermissions();
        String rolePermStr="";
        if(!CollectionUtils.isEmpty(currPermList)){
            StringBuilder sb = new StringBuilder();
            for (Permission perm : currPermList) {
                sb.append(perm.getPermissionName()).append(",");
            }
            rolePermStr=sb.toString();
        }
        //2查找所有的role,jsp遍历roleList,如果登录用户有该权限，就选中
        List<Permission> permissionList = permissionService.findAll();

        ModelAndView mv=new ModelAndView();
        mv.setViewName("role-permission-add");
        mv.addObject("role",role);
        mv.addObject("rolePermStr",rolePermStr);
        mv.addObject("permissionList",permissionList);
        return mv;
    }


    @PostMapping("addPermToRole.do")
    public String addPermToRole(@RequestParam("roleId") String roleId,
                                @RequestParam(name = "ids",required = true)List<String> permIdList){
        //删除原有的关联的role_permission后再新增
        roleService.addPermToRole(roleId,permIdList);
        return "redirect:/role/findAll.do";
    }
}
